import mysql.connector
import sys
import os

# 添加项目根目录到Python路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

# 尝试导入配置
try:
    from app.utils.config import settings
    HAS_SETTINGS = True
except ImportError:
    HAS_SETTINGS = False
    # 如果无法导入配置，使用默认配置
    class Settings:
        DB_HOST = "localhost"
        DB_PORT = 3306
        DB_NAME = "medical_system"
        DB_USER = "root"
        DB_PASSWORD = "root"
    settings = Settings()

def check_db_config():
    """检查数据库配置"""
    print("数据库配置信息:")
    print(f"主机: {settings.DB_HOST}")
    print(f"端口: {settings.DB_PORT}")
    print(f"数据库名: {settings.DB_NAME}")
    print(f"用户名: {settings.DB_USER}")
    
    try:
        conn = mysql.connector.connect(
            host=settings.DB_HOST,
            port=settings.DB_PORT,
            database=settings.DB_NAME,
            user=settings.DB_USER,
            password=settings.DB_PASSWORD
        )
        cursor = conn.cursor()
        
        # 检查最大连接数
        cursor.execute("SHOW VARIABLES LIKE 'max_connections'")
        result = cursor.fetchone()
        print(f"最大连接数: {result[1] if result else '未知'}")
        
        # 检查当前连接数
        cursor.execute("SHOW STATUS LIKE 'Threads_connected'")
        result = cursor.fetchone()
        print(f"当前连接数: {result[1] if result else '未知'}")
        
        cursor.close()
        conn.close()
        print("数据库连接正常")
        return True
    except Exception as e:
        print(f"数据库连接失败: {e}")
        return False

if __name__ == "__main__":
    check_db_config()